<?php

namespace app\user\controller;
use org\Tree;

class Tender extends base
{
//前置方法
    protected $beforeActionList = [
        'chackUser' => ['only' => 'index,edit'],
        'addclass' => ['only' => 'edit'],

        'map' => ['only' => 'index'],
    ];

    protected function chackUser()
    {
        if (empty($this->getLoginUser()->status)) {
            $this->error(lang('Not open this feature'));
        }
    }

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();

            if (isset($data['status']) || !empty($data['status'])) {
                $this->datas ['status'] = $data['status'];
            }

            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
        $this->datas ['uid'] = $this->getLoginUser()->id;
    }

    /**
     * 栏目
     */
    protected function addclass()
    {
        $this->datas['lang'] = cookie('think_var');
        $this->datas['model']='Tender';
        $this->datas['status']='1';
        $nav = model('nav')->where($this->datas)->select();
        $auth_rule = Tree::Forlevel($nav,'|----','id','pid');
        $this->assign('TenderNav', $auth_rule);
        $ss = allow_release_arctype(0,array($this->request->controller()));
        $this->assign('ss',$ss);
    }


    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $data['status'] = 0;
            $data['uid'] = $this->getLoginUser()->id;
            $data['uname'] = $this->getLoginUser()->name;

            if (!empty($data['id'])) {
                return $this->update($data);
            }
            $data['lang']=cookie('think_var');

            $result = $this->model->allowField(true)->save($data);
            if ($result) {
                return ajax_info(lang('Submit successfully awaiting review'), 'y');

            }
            return ajax_info(lang('Submission Failed'));

        }
        $data = $this->model->where('id', $this->request->param('id'))->where('uid', $this->getLoginUser()->id)->find();
        $this->assign(['data' => $data]);
        return $this->fetch();
    }

    /**
     * 查看
     * @param $id
     */
    public function read($id){


        if (intval($id) < 1) {
            $this->error(lang('data error'));
        }

        $data = $this->model->find($id);
        /*列表start*/

        $list =  db('tenderapply')->alias('T')
            ->join('__USER__ U', 'U.id= T.uid')
            ->field('U.name,U.QQ,U.mail,U.mobile,T.center,T.money,T.create_time')
            ->where('T.tid', $id)
            ->paginate()
            ->appends($this->request->param());

        $this->assign([
            'list' => $list,
            'data' => $data,
        ]);

        return $this->fetch();

    }
}
